﻿using CefSharp;
using CefSharp.WinForms;
using cefsharpwinform.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cefsharpwinform
{
    public partial class cefwinform : Form

    {
        private  ChromiumWebBrowser chromeBrowser;
        public cefwinform()
        {
            InitializeComponent();           
            InitializeChromium(); 
        }
     

        //初始化浏览器并启动
        public void InitializeChromium()
        {
            Text = "CefSharp";
            WindowState = FormWindowState.Maximized;
            chromeBrowser = new ChromiumWebBrowser("www.cnki.net");
            toolStripContainer.ContentPanel.Controls.Add(chromeBrowser);  
            
            chromeBrowser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
            chromeBrowser.LoadingStateChanged += OnLoadingStateChanged;
            chromeBrowser.ConsoleMessage += OnBrowserConsoleMessage;          
            chromeBrowser.StatusMessage += OnBrowserStatusMessage;                      
            chromeBrowser.TitleChanged += OnBrowserTitleChanged;
            chromeBrowser.AddressChanged += OnBrowserAddressChanged;
            chromeBrowser.MenuHandler = new MenuHandler(); //屏蔽右键

            var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
               Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
            var bitness = Environment.Is64BitProcess ? "x64" : "x86";
            var environment = String.Format("Environment: {0}", bitness);
            DisplayOutput(string.Format("{0}, {1}", version, environment));
        }
        private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
        {
            var b = ((ChromiumWebBrowser)sender);

            this.InvokeOnUiThreadIfRequired(() => b.Focus());
        }
        private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = args.Address);
        }

        private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => Text = args.Title);
        }
        private void OnBrowserStatusMessage(object sender, StatusMessageEventArgs args)
        {
            this.InvokeOnUiThreadIfRequired(() => statusLabel.Text = args.Value);
        }  
        private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
        {
            DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message));
        }

        public void DisplayOutput(string output)
        {
            this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output);
        }

        private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
        {
            SetCanGoBack(args.CanGoBack);
            SetCanGoForward(args.CanGoForward);

            this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload));
        }

        private void SetCanGoBack(bool canGoBack)
        {
            this.InvokeOnUiThreadIfRequired(() => backButton.Enabled = canGoBack);
        }

        private void SetCanGoForward(bool canGoForward)
        {
            this.InvokeOnUiThreadIfRequired(() => forwardButton.Enabled = canGoForward);
        }

        private void SetIsLoading(bool isLoading)
        {
            goButton.Text = isLoading ?
                "Stop" :
                "Go";
            goButton.Image = isLoading ?
                Properties.Resources.nav_plain_red :
                Properties.Resources.nav_plain_green;

            HandleToolStripLayout();
        }

        private void HandleToolStripLayout()
        {
            var width = toolStrip1.Width;
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                if (item != urlTextBox)
                {
                    width -= item.Width - item.Margin.Horizontal;
                }
            }
            urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            chromeBrowser.Dispose();
            Cef.Shutdown();
            Close();
        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripTextBox1_Click_1(object sender, EventArgs e)
        {

        }

        private void 显示开发工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            chromeBrowser.ShowDevTools();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            chromeBrowser.Dispose();
            Cef.Shutdown();
            Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            chromeBrowser.Back();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            chromeBrowser.Forward();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            LoadUrl(urlTextBox.Text);
        }
        private void LoadUrl(string url)
        {
            if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                chromeBrowser.Load(url);
            }
        }
    }
}
